(function($){
	
	var KEY = "jforest.wizard";
	
	var Wizard = function(element,options){
		this.$el = $(element);
		this.options = $.extend({},{
			step:0,
			isFinish:false
		},options);
		this.steps = [];
		this._analyze();
	}
	
	Wizard.prototype = {
		_analyze:function(){
			var me = this;
			this.$el.find('>li').each(function(i,li){
				me.steps.push(li);
			});
		},
		next:function(callback){
			var opts = this.options,isNext = true;
			if($.isFunction(callback)){
				isNext = callback.apply(this);
			}
			if(isNext == true){
				if(opts.step < this.steps.length - 1){
					$($(this.steps[opts.step]).removeClass('active').addClass('complete').attr('data-target')).removeClass('active'); 
					opts.step = opts.step + 1;
					$($(this.steps[opts.step]).addClass('active').attr('data-target')).addClass('active'); 
				}
				
				if(opts.step == this.steps.length - 1){
					opts.isFinish = true;
				}
			}
		},
		prev:function(callback){
			var opts = this.options;
			if(opts.step > 0){
				$($(this.steps[opts.step]).removeClass('active').attr('data-target')).removeClass('active'); 
				opts.step = opts.step - 1;
				$($(this.steps[opts.step]).removeClass('complete').addClass('active').attr('data-target')).addClass('active'); 
			}
			if($.isFunction(callback)){
				callback.apply(this);
			}
			opts.isFinish = false;
		}
	}
	
	$.fn.jfWizard = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				$this.data(KEY,new Wizard(this,opts)); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
				if(entity === undefined){return;}
				value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfWizard.Constructor = Wizard;
	
})(window.jQuery);